package com.yanqu.road.server.manager.tradewar;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.utils.date.DateHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossTradeWarActivityMgr extends TempMgr {

    private static Map<Integer, ActivityInfo> activityMap;

    private static Map<Integer, List<ActivityConditionInfo>> activityConditionMap;

    @Override
    public boolean reloadData() {
        loadActivity();
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static void loadActivity(){
        String nowTimeStr = DateHelper.getCurrentDateTimeString();
        List<Integer> activityTypeList = new ArrayList<>();
        activityTypeList.add(eActivityType.CrossTradeWarActivity.getValue());
        Map<Integer, ActivityInfo> activityInfoMap = ActivityBussiness.getOpenCrossRankActivityInfoMap(activityTypeList, nowTimeStr);
        List<Integer> activityIdList = new ArrayList<>(activityInfoMap.keySet());
        Map<Integer, List<ActivityConditionInfo>> activityConditionInfoMap = ActivityBussiness.getOpenCrossRankActivityConditionInfoMap(activityIdList);
        activityMap = activityInfoMap;
        activityConditionMap = activityConditionInfoMap;
    }

    /**
     * 获取所有活动
     * @return
     */
    public static Map<Integer, ActivityInfo> getActivityMap() {
        return new ConcurrentHashMap<>(activityMap);
    }

    public static Map<Integer, List<ActivityConditionInfo>> getActivityConditionMap() {
        return new ConcurrentHashMap<>(activityConditionMap);
    }

    public static List<ActivityConditionInfo> getActivityConditionInfoList(int activityId){
        List<ActivityConditionInfo> activityConditionInfoList = activityConditionMap.get(activityId);
        if(null != activityConditionInfoList){
            return new ArrayList<>(activityConditionInfoList);
        }
        return new ArrayList<>();
    }

    public static ActivityConditionInfo getActivityConditionInfo(int activityId, int conditionId){
        List<ActivityConditionInfo> activityConditionInfoList = getActivityConditionInfoList(activityId);
        for(ActivityConditionInfo activityConditionInfo : activityConditionInfoList){
            if(activityConditionInfo.getConditionId() == conditionId){
                return activityConditionInfo;
            }
        }
        return null;
    }

    public static Map<Integer, ActivityInfo> getOpenActivityMap(){
        Map<Integer, ActivityInfo> dataMap = new ConcurrentHashMap<>();
        for(ActivityInfo activityInfo : CrossTradeWarActivityMgr.getActivityMap().values()){
            if(ActivityHelper.activityInShowTime(activityInfo)){
                dataMap.put(activityInfo.getActivityId(), activityInfo);
            }
        }
        return dataMap;
    }

    public static ActivityInfo getActivityInfo(int activityId){
        return activityMap.get(activityId);
    }

    /**
     * 获取活动信息，如果没有从数据库加载
     * @param activityId
     * @return
     */
    public static ActivityInfo getActivityInfoIfNotLoadDb(int activityId){
        if(!activityMap.containsKey(activityId)){
            loadActivity();
        }
        return activityMap.get(activityId);
    }
}
